---
title: "Do Intergovernmental Organizations Have a Socialization Effect on Member State Preferences? Evidence from the UN General Debate"
author:
- affiliation: Loughborough University
  email: n.chelotti@lboro.ac.uk
  name: Nicola Chelotti
- affiliation: University of Birmingham
  email: n.dasandi@bham.ac.uk
  name: Niheer Dasandi
- affiliation: Data Science Lab at the Hertie School
  email: jankin@hertie-school.org
  name: Slava Jankin
date: 26 October 2018
output:
  html_notebook:
    toc: yes
  html_document: default
  pdf_document: 
    toc: yes
  word_document: 
    toc: yes
#biblio-style: apsr
#bibliography: eu.bib
thanks: Forthcoming in ISQ.
abstract: The question of whether Intergovernmental Organizations (IGOs) have a socialization effect on member state preferences is central to International Relations. However, empirical studies have struggled to separate the socializing effects of IGOs on preferences from the coercion and incentives associated with IGOs that may lead to foreign policy alignment without altering preferences. This paper addresses this issue. We adopt a novel approach to measuring state preferences by applying text analytic methods to country statements in the annual UN General Debate (UNGD). The absence of inter-state coordination with UNGD statements makes them particularly well suited for testing socialization effects on state preferences. We focus on the European Union (EU), enabling us to incorporate the pre-accession period -- when states have the strongest incentives for foreign policy alignment -- into our analysis. The results of our analysis show that EU membership has a socialization effect that produces preference convergence, controlling for coercion and incentive effects. 
---


Quanteda is notoriously unstable in terms of functions being updated frequently and breaking pipelines. Hence hard requiring a stable version of quanteda that was used for initial analysis for full replicability.

```{r eval=FALSE, include=FALSE}
require(devtools)
install_version("quanteda", version = "1.2.0", repos = "http://cran.us.r-project.org")
```


```{r, message=FALSE}
library(readtext)
library(quanteda)
library(tidyverse)
library(stm)
library(tidytext)
library(haven)
library(data.table)
library(ggridges)
library(viridis)
```


UNGD data are available on the Harvard Dataverse at https://doi.org/10.7910/DVN/0TJX8Y

```{r}

DATA_DIR <- "~/Dropbox/Research/UNGDC projects/UN Data/" 

ungd_files <- readtext(paste0(DATA_DIR, "TXT/*"), 
                                 docvarsfrom = "filenames", 
                                 dvsep="_", 
                                 docvarnames = c("Country", "Session", "Year"))


ungd_corpus <- corpus(ungd_files, text_field = "text") 

```



#Analysis


###Wordscore based positions


```{r EU president, include=FALSE}
presidency <- readxl::read_excel("../presidency.xlsx")

```


```{r dropping features}
tok <- tokens(ungd_corpus, what = "word",
              remove_punct = TRUE,
              remove_symbols = TRUE,
              remove_numbers = TRUE,
              remove_twitter = TRUE,
              remove_url = TRUE,
              remove_hyphens = TRUE,
              verbose = TRUE)


dfm <- dfm(tok, 
           tolower = TRUE,
           remove=stopwords("english"),
           stem=TRUE, 
           verbose = TRUE)


#Removing any digits. `dfm` picks up any separated digits, not digits that are part of tokens.
#Removing any punctuation. `dfm` picks up any punctuation unless it's part of a token.
#Removing any tokens less than four characters.
dfm.m <- dfm_select(dfm, c("[\\d-]", "[[:punct:]]", "^.{1,3}$"), selection = "remove", 
                    valuetype="regex", verbose = TRUE)

#Dropping words that appear less than 5 times and in less than 3 documents.
dfm.trim <- dfm_trim(dfm.m, min_termfreq = 5, min_docfreq = 3, verbose = TRUE)

#group documents by year in dfm
dfm.year <- dfm_group(dfm.trim, dfm.trim@docvars$Year)

# get names of variables to be dropped
dfm.year.trimmed <- dfm_trim(dfm.year, max_docfreq=1, verbose = TRUE)
df.year.trimmed <- as.data.frame(dfm.year.trimmed)

variables_drop <- names(df.year.trimmed)
variables_drop <- variables_drop[2:length(variables_drop)]


#group documents by year in dfm
dfm.year_all <- dfm_group(dfm.m, dfm@docvars$Year)

# get names of variables to be dropped
df.year.trimmed_all <- as.data.frame(dfm_trim(dfm.year_all, max_docfreq=1))


```




```{r PresidencyUSA, include=FALSE}

presus <- data.frame()

presus_words <- data.frame()

for (i in c(1970:2017)) {
  
#Creating corpus for each year
ungdc.i <- corpus_subset(ungd_corpus, Year==i)

tok <- tokens(ungdc.i, what = "word",
              remove_punct = TRUE,
              remove_symbols = TRUE,
              remove_numbers = TRUE,
              remove_twitter = TRUE,
              remove_url = TRUE,
              remove_hyphens = TRUE,
              verbose = TRUE)


dfm <- dfm(tok, 
           tolower = TRUE,
           remove=stopwords("english"),
           stem=TRUE, 
           verbose = TRUE)


#Removing any digits. `dfm` picks up any separated digits, not digits that are part of tokens.
#Removing any punctuation. `dfm` picks up any punctuation unless it's part of a token.
#Removing any tokens less than four characters.
dfm.m <- dfm_select(dfm, c("[\\d-]", "[[:punct:]]", "^.{1,3}$"), selection = "remove", 
                    valuetype="regex", verbose = TRUE)


#Dropping words that appear less than 5 times and in less than 3 documents.
dfm.trim <- dfm_trim(dfm.m, min_termfreq = 5, min_docfreq = 3, verbose = TRUE)


# dropping words that only appear in one year
dfm.trim <- dfm_select(dfm.trim, variables_drop, selection = "remove", valuetype="fixed", verbose = TRUE)


#tfidf weighting
dfm.w <- dfm_tfidf(dfm.trim)


#holders for country names in distance measures below
pres <- paste0(presidency$Country_alt[presidency$Year==i])


#Reference scores
refscores <- rep(NA,nrow(dfm.w))

refscores[dfm.w@docvars$Country=="USA"] <- -1
refscores[dfm.w@docvars$Country==pres ] <- 1


#Wordscore model
ws <- textmodel_wordscores(dfm.w, refscores, scale="linear", smooth=0.5)
wordscore <- predict(ws, rescaling="none")

#Writing the results into data frame
wordscores.i <- data.frame(cbind(docvars(ungdc.i), wordscore))

presus <- rbind(presus,wordscores.i)


words <- as.data.frame(coef(ws))
words$Year <- i
words$words <- row.names(words)

head_w <- words %>% top_n(5, coef(ws))
tail_w <- words %>% top_n(-5, coef(ws))

words.i <- rbind(head_w, tail_w)

presus_words <- rbind(presus_words,words.i)

}

```






```{r PresidencyRussia, include=FALSE}
#EU president

presrus <- data.frame()

presrus_words <- data.frame()

for (i in 1971:2017) {
  
#Creating corpus for each year
ungdc.i <- corpus_subset(ungd_corpus, Year==i)

tok <- tokens(ungdc.i, what = "word",
              remove_punct = TRUE,
              remove_symbols = TRUE,
              remove_numbers = TRUE,
              remove_twitter = TRUE,
              remove_url = TRUE,
              remove_hyphens = TRUE,
              verbose = TRUE)


dfm <- dfm(tok, 
           tolower = TRUE,
           remove=stopwords("english"),
           stem=TRUE, 
           verbose = TRUE)

#Removing any digits. `dfm` picks up any separated digits, not digits that are part of tokens.
#Removing any punctuation. `dfm` picks up any punctuation unless it's part of a token.
#Removing any tokens less than four characters.
dfm.m <- dfm_select(dfm, c("[\\d-]", "[[:punct:]]", "^.{1,3}$"), selection = "remove", 
                    valuetype="regex", verbose = TRUE)


#Dropping words that appear less than 5 times and in less than 3 documents.
dfm.trim <- dfm_trim(dfm.m, min_termfreq = 5, min_docfreq = 3, verbose = TRUE)

# dropping words that only appear in one year
dfm.trim <- dfm_select(dfm.trim, variables_drop, selection = "remove", valuetype="fixed", verbose = TRUE)

#tfidf weighting
dfm.w <- dfm_tfidf(dfm.trim)

#holders for country names in distance measures below
pres <- paste0(presidency$Country_alt[presidency$Year==i])


#Reference scores
refscores <- rep(NA,nrow(dfm.w))

refscores[dfm.w@docvars$Country=="RUS"] <- -1
refscores[dfm.w@docvars$Country==pres ] <- 1

#Wordscore model
ws <- textmodel_wordscores(dfm.w, refscores, scale="linear", smooth=0.5)
wordscore <- predict(ws, rescaling="none")

#Writing the results into data frame
wordscores.i <- data.frame(cbind(docvars(ungdc.i), wordscore))

presrus <- rbind(presrus,wordscores.i)

words <- as.data.frame(coef(ws))
words$Year <- i
words$words <- row.names(words)

head_w <- words %>% top_n(5, coef(ws))
tail_w <- words %>% top_n(-5, coef(ws))

words.i <- rbind(head_w, tail_w)

presrus_words <- rbind(presrus_words,words.i)

}

```




```{r}
#Logicals for EU member states
EU <- c("BEL", "FRA", "DEU", "ITA", "LUX", "NLD")
wave1 <- c("DNK", "IRL", "GBR")
wave2 <- "GRC" 
wave3 <- c("ESP", "PRT") 
wave4 <- c("AUT", "FIN", "SWE") 
wave5 <- c("CZE", "HUN", "POL", "EST", "LVA", "LTU", "CYP", "MLT", "SVK", "SVN")
wave6 <- c("BGR", "ROU") 
wave7 <- "HRV" 
```





```{r}
presus2 <- presus

presus2 <- presus2 %>% mutate(is.eu = Country %in% EU)

presus2$waves <- presus2$is.eu

presus2$waves <- presus2$is.eu

#first wave
presus2$is.eu[presus2$Country== "DNK" & presus2$Year >1972] <- TRUE
presus2$is.eu[presus2$Country== "IRL" & presus2$Year >1972] <- TRUE
presus2$is.eu[presus2$Country== "GBR" & presus2$Year >1972] <- TRUE

#second wave
presus2$is.eu[presus2$Country== "GRC" & presus2$Year >1980] <- TRUE

#third wave
presus2$is.eu[presus2$Country== "ESP" & presus2$Year >1985] <- TRUE
presus2$is.eu[presus2$Country== "PRT" & presus2$Year >1985] <- TRUE

#fourth wave
presus2$is.eu[presus2$Country== "AUT" & presus2$Year >1994] <- TRUE
presus2$is.eu[presus2$Country== "FIN" & presus2$Year >1994] <- TRUE
presus2$is.eu[presus2$Country== "SWE" & presus2$Year >1994] <- TRUE

#fifth wave
presus2$is.eu[presus2$Country== "CZE" & presus2$Year >2003] <- TRUE
presus2$is.eu[presus2$Country== "HUN" & presus2$Year >2003] <- TRUE
presus2$is.eu[presus2$Country== "POL" & presus2$Year >2003] <- TRUE
presus2$is.eu[presus2$Country== "EST" & presus2$Year >2003] <- TRUE
presus2$is.eu[presus2$Country== "LVA" & presus2$Year >2003] <- TRUE
presus2$is.eu[presus2$Country== "LTU" & presus2$Year >2003] <- TRUE
presus2$is.eu[presus2$Country== "CYP" & presus2$Year >2003] <- TRUE
presus2$is.eu[presus2$Country== "MLT" & presus2$Year >2003] <- TRUE
presus2$is.eu[presus2$Country== "SVK" & presus2$Year >2003] <- TRUE
presus2$is.eu[presus2$Country== "SVN" & presus2$Year >2003] <- TRUE

#sixth wave
presus2$is.eu[presus2$Country== "BGR" & presus2$Year >2006] <- TRUE
presus2$is.eu[presus2$Country== "ROU" & presus2$Year >2006] <- TRUE

#seventh wave
presus2$is.eu[presus2$Country== "HRV" & presus2$Year >2012] <- TRUE
```




```{r}
presrus2 <- presrus

presrus2 <- presrus2 %>% mutate(is.eu = Country %in% EU)

#first wave
presrus2$is.eu[presrus2$Country== "DNK" & presrus2$Year >1972] <- TRUE
presrus2$is.eu[presrus2$Country== "IRL" & presrus2$Year >1972] <- TRUE
presrus2$is.eu[presrus2$Country== "GBR" & presrus2$Year >1972] <- TRUE

#second wave
presrus2$is.eu[presrus2$Country== "GRC" & presrus2$Year >1980] <- TRUE

#third wave
presrus2$is.eu[presrus2$Country== "ESP" & presrus2$Year >1985] <- TRUE
presrus2$is.eu[presrus2$Country== "PRT" & presrus2$Year >1985] <- TRUE

#fourth wave
presrus2$is.eu[presrus2$Country== "AUT" & presrus2$Year >1994] <- TRUE
presrus2$is.eu[presrus2$Country== "FIN" & presrus2$Year >1994] <- TRUE
presrus2$is.eu[presrus2$Country== "SWE" & presrus2$Year >1994] <- TRUE

#fifth wave
presrus2$is.eu[presrus2$Country== "CZE" & presrus2$Year >2003] <- TRUE
presrus2$is.eu[presrus2$Country== "HUN" & presrus2$Year >2003] <- TRUE
presrus2$is.eu[presrus2$Country== "POL" & presrus2$Year >2003] <- TRUE
presrus2$is.eu[presrus2$Country== "EST" & presrus2$Year >2003] <- TRUE
presrus2$is.eu[presrus2$Country== "LVA" & presrus2$Year >2003] <- TRUE
presrus2$is.eu[presrus2$Country== "LTU" & presrus2$Year >2003] <- TRUE
presrus2$is.eu[presrus2$Country== "CYP" & presrus2$Year >2003] <- TRUE
presrus2$is.eu[presrus2$Country== "MLT" & presrus2$Year >2003] <- TRUE
presrus2$is.eu[presrus2$Country== "SVK" & presrus2$Year >2003] <- TRUE
presrus2$is.eu[presrus2$Country== "SVN" & presrus2$Year >2003] <- TRUE

#sixth wave
presrus2$is.eu[presrus2$Country== "BGR" & presrus2$Year >2006] <- TRUE
presrus2$is.eu[presrus2$Country== "ROU" & presrus2$Year >2006] <- TRUE

#seventh wave
presrus2$is.eu[presrus2$Country== "HRV" & presrus2$Year >2012] <- TRUE
```



```{r}
presus2 <-  mutate(presus2, originals = Country %in% EU)
presus2 <-  mutate(presus2, wave1 = Country %in% wave1)
presus2 <-  mutate(presus2, wave2 = Country %in% wave2)
presus2 <-  mutate(presus2, wave3 = Country %in% wave3)
presus2 <-  mutate(presus2, wave4 = Country %in% wave4)
presus2 <-  mutate(presus2, wave5 = Country %in% wave5)
presus2 <-  mutate(presus2, wave6 = Country %in% wave6)
presus2 <-  mutate(presus2, wave7 = Country %in% wave7)
```



```{r}
names(presus2)[4] <- "PRESUS_wscore"
names(presrus2)[4] <- "PRESRUS_wscore"
names(presus2)[5] <- "EU"

scores <- full_join(presus2, presrus2, by=c("Country", "Year"))

```




```{r}
scores <-  mutate(scores, eu9 = originals)
scores$eu9[scores$Country== "DNK" & scores$Year >1972] <- TRUE
scores$eu9[scores$Country== "IRL" & scores$Year >1972] <- TRUE
scores$eu9[scores$Country== "GBR" & scores$Year >1972] <- TRUE

scores <-  mutate(scores, eu12 = eu9)
scores$eu12[scores$Country== "GRC" & scores$Year >1980] <- TRUE
scores$eu12[scores$Country== "ESP" & scores$Year >1985] <- TRUE
scores$eu12[scores$Country== "PRT" & scores$Year >1985] <- TRUE

scores <-  mutate(scores, eu15 = eu12)
scores$eu15[scores$Country== "AUT" & scores$Year >1994] <- TRUE
scores$eu15[scores$Country== "FIN" & scores$Year >1994] <- TRUE
scores$eu15[scores$Country== "SWE" & scores$Year >1994] <- TRUE

```

Calculating average scores and standard deviations for EU member states.



```{r}
eu6 <- filter(scores, originals==TRUE) %>% 
  group_by( . ,  Year) %>%  
  summarise(. , score_presus_eu6 = mean(PRESUS_wscore), sd_presus_eu6 = sd(PRESUS_wscore), 
            score_presrus_eu6 = mean(PRESRUS_wscore), sd_presrus_eu6 = sd(PRESRUS_wscore))

eu10 <- filter(scores, wave5==TRUE) %>% 
  group_by( . ,  Year) %>%  
  summarise(. , score_presus_eu10 = mean(PRESUS_wscore, na.rm = TRUE), sd_presus_eu10 = sd(PRESUS_wscore, na.rm = TRUE), score_presrus_eu10 = mean(PRESRUS_wscore, na.rm = TRUE), sd_presrus_eu10 = sd(PRESRUS_wscore, na.rm = TRUE))

eu9 <- filter(scores, eu9==TRUE) %>% 
  group_by( . ,  Year) %>%  
  summarise(. , score_presus_eu9 = mean(PRESUS_wscore), sd_presus_eu9 = sd(PRESUS_wscore), 
            score_presrus_eu9 = mean(PRESRUS_wscore), sd_presrus_eu9 = sd(PRESRUS_wscore))

eu12 <- filter(scores, eu12==TRUE) %>% 
  group_by( . ,  Year) %>%  
  summarise(. , score_presus_eu12 = mean(PRESUS_wscore), sd_presus_eu12 = sd(PRESUS_wscore), 
            score_presrus_eu12 = mean(PRESRUS_wscore), sd_presrus_eu12 = sd(PRESRUS_wscore))

eu15 <- filter(scores, eu15==TRUE) %>% 
  group_by( . ,  Year) %>%  
  summarise(. , score_presus_eu15 = mean(PRESUS_wscore), sd_presus_eu15 = sd(PRESUS_wscore), 
            score_presrus_eu15 = mean(PRESRUS_wscore), sd_presrus_eu15 = sd(PRESRUS_wscore))

eu_presus <- summarise(group_by(filter(scores, EU==TRUE), Year), 
                      score_presus = mean(PRESUS_wscore, na.rm = TRUE), 
                      sd_presus = sd(PRESUS_wscore, na.rm = TRUE))

eu_presrus <- summarise(group_by(filter(scores, EU==TRUE), Year), 
                      score_presrus = mean(PRESRUS_wscore, na.rm = TRUE), 
                      sd_presrus = sd(PRESRUS_wscore, na.rm = TRUE))

```


```{r}
av_scores <- left_join(eu6, eu9, by="Year" )
av_scores <- left_join(av_scores, eu12, by="Year" )
av_scores <- left_join(av_scores, eu15, by="Year" )
av_scores <- left_join(av_scores, eu10, by="Year" )
av_scores <- left_join(av_scores, eu_presus, by="Year" )
av_scores <- left_join(av_scores, eu_presrus, by="Year" )

```


Combining files for output:

```{r}
readr::write_csv(av_scores, "average_estimates_v1_2_jeffreys.csv")

readr::write_csv(scores,  "estimates_v1_2_jeffreys.csv")

readr::write_csv(presrus_words,  "presrus_words.csv")
readr::write_csv(presus_words,  "presus_words.csv")

```






####################
##### Figure 1
####################






```{r}

ggplot() + 
  geom_density_ridges_gradient(data = subset(scores, (EU==TRUE | Country=="USA" | Country=="EU") & Year<2015), 
                               aes(x=PRESUS_wscore, y= Year, group = Year,  fill = ..x..), 
                               rel_min_height = 0.001, scale = 5) +
  scale_fill_viridis(name = "Wordscore", option = "C", alpha = .5) +
  theme_bw() + theme(text = element_text(size=12)) +
  ggtitle("EU (+) - USA (-) dimension") + 
  xlab("Wordscore positions") + ylab("Year") +
   scale_y_discrete(limits = c(1970, 1980, 1990, 2000, 2010, 2014)) 

ggsave("presus_density_NEW.pdf")
```




```{r}

ggplot() + 
  geom_density_ridges_gradient(data = subset(scores, (EU==TRUE | Country=="RUS" | Country=="EU") & Year<2015), 
                               aes(x=PRESRUS_wscore, y= Year, group = Year,  fill = ..x..), 
                               rel_min_height = 0.001, scale = 5) +
  scale_fill_viridis(name = "Wordscore", option = "C", alpha = .5) +
  theme_bw() + theme(text = element_text(size=12)) +
  ggtitle("EU (+) - RUS (-) dimension") + 
  xlab("Wordscore positions") + ylab("Year") +
   scale_y_discrete(limits = c(1970, 1980, 1990, 2000, 2010, 2014)) 

ggsave("presrus_density_NEW.pdf")
```








######################
##### Figure 4
######################


```{r}
#Plot of average scores
ggplot(data = filter(scores, wave5==TRUE & Year>1990), 
       aes(x=Year, y=PRESRUS_wscore, group = Country, colour = Country)) +
  geom_smooth(se=FALSE) +
  ylab("Wordscore") +
  ggtitle("EU (+) - Russia (-) dimension: EU10") +
  theme_bw()

ggsave("SCORE_EU10_presrus.pdf")
```



```{r}
#Plot of average scores
ggplot(data = filter(scores, wave5==TRUE & Year>1990), 
       aes(x=Year, y=PRESUS_wscore, group = Country, colour = Country)) +
  geom_smooth(se=FALSE) +
  ylab("Wordscore") +
  ggtitle("EU (+) - USA (-) dimension: EU10") +
  theme_bw()

ggsave("SCORE_EU10_presus.pdf")
```








#######################
###### Figure 3
#######################

```{r}

positive_cw <- presus_words %>% 
  filter(Year>1970 & Year<1991) %>% 
  filter(ws>0) %>% 
  arrange(desc(ws)) %>% 
  distinct(words, .keep_all = TRUE)

ggplot(positive_cw[1:30, ], aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): Cold War Period") +
  theme_bw()

ggsave("PRESUS_positive_CW.pdf")

positive_pcw <- presus_words %>% 
  filter(Year>1990 & Year<2015) %>% 
  filter(ws>0) %>% 
  arrange(desc(ws)) %>% 
  distinct(words, .keep_all = TRUE)

ggplot(positive_pcw[1:30, ], aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): Post-Cold War Period") +
  theme_bw()

ggsave("PRESUS_positive_PCW.pdf")



negative_cw <- presus_words %>% 
  filter(Year>1970 & Year<1991) %>% 
  filter(ws<0) %>% 
  arrange(desc(-ws)) %>% 
  distinct(words, .keep_all = TRUE)


ggplot(negative_cw[1:30, ], aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): : Cold War Period") +
  theme_bw()

ggsave("PRESUS_negative_CW.pdf")


negative_pcw <- presus_words %>% 
  filter(Year>1990 & Year<2015) %>% 
  filter(ws<0) %>% 
  arrange(desc(-ws)) %>% 
  distinct(words, .keep_all = TRUE)


ggplot(negative_pcw[1:30, ], aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): : Post-Cold War Period") +
  theme_bw()

ggsave("PRESUS_negative_PCW.pdf")


```








#######################
###### Figures 6-10 Supplementary Materials
#######################



```{r}

positive_annual <- presus_words %>% 
  filter(Year>1970 & Year<1981) %>% 
  filter(ws>0) %>% 
  arrange(Year, desc(ws)) 

ggplot(positive_annual, aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): 1971-1980") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("PRESUS_positive_1971-1980.pdf")

positive_annual <- presus_words %>% 
  filter(Year>1980 & Year<1991) %>% 
  filter(ws>0) %>% 
  arrange(Year, desc(ws)) 

ggplot(positive_annual, aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): 1981-1990") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("PRESUS_positive_1981-1990.pdf")



positive_annual <- presus_words %>% 
  filter(Year>1990 & Year<2001) %>% 
  filter(ws>0) %>% 
  arrange(Year, desc(ws)) 

ggplot(positive_annual, aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): 1991-2000") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("PRESUS_positive_1991-2000.pdf")


positive_annual <- presus_words %>% 
  filter(Year>2000 & Year<2011) %>% 
  filter(ws>0) %>% 
  arrange(Year, desc(ws)) 

ggplot(positive_annual, aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): 2001-2010") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("PRESUS_positive_2001-2010.pdf")



positive_annual <- presus_words %>% 
  filter(Year>2010 & Year<2015) %>% 
  filter(ws>0) %>% 
  arrange(Year, desc(ws)) 

ggplot(positive_annual, aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): 2011-2014") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("PRESUS_positive_2011-2014.pdf")




################### negative

negative_annual <- presus_words %>% 
  filter(Year>1970 & Year<1981) %>% 
  filter(ws<0) %>% 
  arrange(Year, desc(-ws)) 

ggplot(negative_annual, aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): 1971-1980") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("PRESUS_negative_1971-1980.pdf")

negative_annual <- presus_words %>% 
  filter(Year>1980 & Year<1991) %>% 
  filter(ws<0) %>% 
  arrange(Year, desc(-ws)) 

ggplot(negative_annual, aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): 1981-1990") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("PRESUS_negative_1981-1990.pdf")



negative_annual <- presus_words %>% 
  filter(Year>1990 & Year<2001) %>% 
  filter(ws<0) %>% 
  arrange(Year, desc(-ws)) 

ggplot(negative_annual, aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): 1991-2000") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("PRESUS_negative_1991-2000.pdf")


negative_annual <- presus_words %>% 
  filter(Year>2000 & Year<2011) %>% 
  filter(ws<0) %>% 
  arrange(Year, desc(-ws)) 

ggplot(negative_annual, aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): 2001-2010") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("PRESUS_negative_2001-2010.pdf")



negative_annual <- presus_words %>% 
  filter(Year>2010 & Year<2015) %>% 
  filter(ws<0) %>% 
  arrange(Year, desc(-ws)) 

ggplot(negative_annual, aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - USA (-): 2011-2014") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("PRESUS_negative_2011-2014.pdf")



```








#######################
###### Figure 2
#######################

```{r}

positive_cw <- presrus_words %>% 
  filter(Year>1970 & Year<1991) %>% 
  filter(ws>0) %>% 
  arrange(desc(ws)) %>% 
  distinct(words, .keep_all = TRUE)

ggplot(positive_cw[1:30, ], aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): Cold War Period") +
  theme_bw()

ggsave("PRESRUS_positive_CW.pdf")

positive_pcw <- presrus_words %>% 
  filter(Year>1990 & Year<2015) %>% 
  filter(ws>0) %>% 
  arrange(desc(ws)) %>% 
  distinct(words, .keep_all = TRUE)

ggplot(positive_pcw[1:30, ], aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): Post-Cold War Period") +
  theme_bw()

ggsave("PRESRUS_positive_PCW.pdf")



negative_cw <- presrus_words %>% 
  filter(Year>1970 & Year<1991) %>% 
  filter(ws<0) %>% 
  arrange(desc(-ws)) %>% 
  distinct(words, .keep_all = TRUE)


ggplot(negative_cw[1:30, ], aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): : Cold War Period") +
  theme_bw()

ggsave("PRESRUS_negative_CW.pdf")


negative_pcw <- presrus_words %>% 
  filter(Year>1990 & Year<2015) %>% 
  filter(ws<0) %>% 
  arrange(desc(-ws)) %>% 
  distinct(words, .keep_all = TRUE)


ggplot(negative_pcw[1:30, ], aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): : Post-Cold War Period") +
  theme_bw()

ggsave("PRESRUS_negative_PCW.pdf")


```


#######################
###### Figures 1-5 Supplementary Materials
#######################

```{r}

positive_annual <- presrus_words %>% 
  filter(Year>1970 & Year<1981) %>% 
  filter(ws>0) %>% 
  arrange(Year, desc(ws)) 

ggplot(positive_annual, aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): 1971-1980") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("presrus_positive_1971-1980.pdf")

positive_annual <- presrus_words %>% 
  filter(Year>1980 & Year<1991) %>% 
  filter(ws>0) %>% 
  arrange(Year, desc(ws)) 

ggplot(positive_annual, aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): 1981-1990") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("presrus_positive_1981-1990.pdf")



positive_annual <- presrus_words %>% 
  filter(Year>1990 & Year<2001) %>% 
  filter(ws>0) %>% 
  arrange(Year, desc(ws)) 

ggplot(positive_annual, aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): 1991-2000") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("presrus_positive_1991-2000.pdf")


positive_annual <- presrus_words %>% 
  filter(Year>2000 & Year<2011) %>% 
  filter(ws>0) %>% 
  arrange(Year, desc(ws)) 

ggplot(positive_annual, aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): 2001-2010") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("presrus_positive_2001-2010.pdf")



positive_annual <- presrus_words %>% 
  filter(Year>2010 & Year<2015) %>% 
  filter(ws>0) %>% 
  arrange(Year, desc(ws)) 

ggplot(positive_annual, aes(x = reorder(words, ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): 2011-2014") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("presrus_positive_2011-2014.pdf")




################### negative

negative_annual <- presrus_words %>% 
  filter(Year>1970 & Year<1981) %>% 
  filter(ws<0) %>% 
  arrange(Year, desc(-ws)) 

ggplot(negative_annual, aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): 1971-1980") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("presrus_negative_1971-1980.pdf")

negative_annual <- presrus_words %>% 
  filter(Year>1980 & Year<1991) %>% 
  filter(ws<0) %>% 
  arrange(Year, desc(-ws)) 

ggplot(negative_annual, aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): 1981-1990") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("presrus_negative_1981-1990.pdf")



negative_annual <- presrus_words %>% 
  filter(Year>1990 & Year<2001) %>% 
  filter(ws<0) %>% 
  arrange(Year, desc(-ws)) 

ggplot(negative_annual, aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): 1991-2000") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("presrus_negative_1991-2000.pdf")


negative_annual <- presrus_words %>% 
  filter(Year>2000 & Year<2011) %>% 
  filter(ws<0) %>% 
  arrange(Year, desc(-ws)) 

ggplot(negative_annual, aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): 2001-2010") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("presrus_negative_2001-2010.pdf")



negative_annual <- presrus_words %>% 
  filter(Year>2010 & Year<2015) %>% 
  filter(ws<0) %>% 
  arrange(Year, desc(-ws)) 

ggplot(negative_annual, aes(x = reorder(words, -ws), y = ws)) +
    geom_point() +
    coord_flip() +
  facet_grid(cols = vars(Year)) +
    labs(x = NULL, y = "Wordscore") + ggtitle("EU (+) - RUS (-): 2011-2014") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 

ggsave("presrus_negative_2011-2014.pdf")



```














